package com.lxb;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 中序遍历 压栈
 */
public class InorderTraversal2 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        TreeNode l_num1 = new TreeNode(3);
        TreeNode l_num2 = new TreeNode(1);
        TreeNode r_num1 = new TreeNode(8);
        TreeNode r_num2 = new TreeNode(4);

        root.left = l_num1;
        root.right = r_num1;
        root.left.left = l_num2;
        root.left.right = r_num2;

        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();

        while (root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);// 压栈
                root = root.left;
            }
            root = stack.pop();// 出栈
            list.add(root.val);
            root = root.right;
        }

        System.out.println(list.toString());


    }
}
